<template>
    <div class="search">
        <el-autocomplete @select="goDetail" :trigger-on-focus="false" :fetch-suggestions="fetchData" v-model="hosname"
            clearable class="inline-input w-50" placeholder="请输入医院的名称" />
        <el-button type="primary" size="default" :icon="Search"></el-button>
    </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
// 按需引入icon图标
import { Search } from "@element-plus/icons-vue";
// 引入API
import { reqHospitalInfo } from '@/api/home/index'
// 引入ts类型
import { HospitalInfoResponseData } from '@/api/home/type'
// 引入路由器
import { useRouter } from "vue-router";

// 创建路由器对象
let router = useRouter();
//收集搜索的关键字(医院的名字)
let hosname = ref<string>('')
// 搜索框的处理事件
const fetchData = async (keyword: string, callback: any) => {
    //当用户输入完关键字次函数会执行一次，发请求获取需要展示的数据即可
    const result: HospitalInfoResponseData = await reqHospitalInfo(keyword)
    // 整理数据，变成组件需要数据格式
    let showData = result.data.map((item: any) => {
        return {
            value: item.hosname,
            hoscode: item.hoscode
        }
    })
    // 把收集搜索到的值交给回调函数
    callback(showData);
}
// 点击推荐项目进入医院详情页  这里的item是@select自动带入的
const goDetail = (item: any) => {
    // 跳转到医院详情页
    router.push({ path: '/hospital/register', query: { hoscode: item.hoscode } })
}
</script>

<script lang="ts">
export default {
    name: 'Search'
}
</script>

<style scoped lang="scss">
.search {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px 0px;

    ::v-deep(.el-input__wrapper) {
        width: 800px;
        height: 40px;
    }

    ::v-deep(.el-button) {
        height: 40px;
        width: 50px;
    }
}
</style>